package com.bajiao.service.visitor.impl;

import com.bajiao.common.constant._Whole;
import com.bajiao.common.util.DateUtil;
import com.bajiao.common.util.DateUtils;
import com.bajiao.common.util.RedisUtil;
import com.bajiao.entity.visitor.Visitor;
import com.bajiao.mapper.visitor.VisitorMapper;
import com.bajiao.service.visitor.VisitorService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 访客记录
 * @Author: FanJq
 * @Date:   2024-12-13
 * @Version: V1.0
 */
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements VisitorService {

    @Resource
    private RedisUtil redisUtil;

    /**
     * @Description
     * 记录访客
     * @date 2024/12/13 10:30
     * @author FanJq
     * @param
     * @return
    */
    @Override
    @Async("taskExecutor")
    public void record() {
        String nowDate = DateUtils.date_sdf.get().format(new Date());
        if (redisUtil.hasKey(_Whole.visitorKey + nowDate)) {
            //@create by FanJq 2024/12/13 10:34 存在key直接累加
            super.lambdaUpdate().setSql("num = num + 1").eq(Visitor::getCreateTime, nowDate).update();
        } else {
            //@create by FanJq 2024/12/13 10:35 不存在key新增
            super.save(new Visitor());
            redisUtil.set(_Whole.visitorKey + nowDate, nowDate, DateUtils.getTodaySurplusSeconds());
        }
    }
}
